package com.study.d014Bounds;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
*@description  设置和获取组件的宽和高
*@author xtayhame
*date 2020年3月4日
*version
*since v1.0
*/
public class PrefWidthAndHeight extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();

		HBox hBox = new HBox();

		Button b1 = new Button("这是我的第一个按钮");

		//有些组件如Rectangle默认是没有宽和高的
		Rectangle rectangle = new Rectangle();

		hBox.getChildren().add(b1);

		anchorPane.getChildren().addAll(hBox,rectangle);

		stage.setScene(new Scene(anchorPane, 400, 500));

		stage.show();

		//在没有设置Button宽和高的情况下
		System.out.println("getPrefWidth="+b1.getPrefWidth() + "   getPrefHeight=" +b1.getPrefHeight());   //getPrefWidth=-1.0   getPrefHeight=-1.0

		System.out.println("getWidth="+b1.getWidth() + "   getHeight=" +b1.getHeight());  				   //getWidth=124.0   getHeight=23.0

		b1.setPrefWidth(50);
		b1.setPrefHeight(50);

		//在设置Button宽和高的情况下
		System.out.println("getPrefWidth="+b1.getPrefWidth() + "   getPrefHeight=" +b1.getPrefHeight());   //getPrefWidth=50.0   getPrefHeight=50.0

		System.out.println("getWidth="+b1.getWidth() + "   getHeight=" +b1.getHeight());  				   //getWidth=124.0   getHeight=23.0

		System.out.println("父组件是否会自动设置宽和高："+b1.isResizable());
		System.out.println("父组件是否会自动设置宽和高："+rectangle.isResizable());

		b1.setPrefWidth(Button.USE_COMPUTED_SIZE);

		System.out.println("getPrefWidth="+b1.getPrefWidth() + "   getPrefHeight=" +b1.getPrefHeight());   //getPrefWidth=50.0   getPrefHeight=50.0

		System.out.println("getWidth="+b1.getWidth() + "   getHeight=" +b1.getHeight());  				   //getWidth=124.0   getHeight=23.0

		System.out.println(anchorPane.getChildren().size());

		System.out.println("rectangle.getWidth="+rectangle.getWidth() + ",  rectangle.getHeight=" + rectangle.getHeight());
	}

}
